package StudyCode.summary.mycollection;

import java.util.Comparator;
import java.util.TreeMap;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetTreeMapByComparator {
	public static void main(String[] argv) throws Exception {
		System.out.println(getTreeMap());
	}

	public static TreeMap<Object, Object> getTreeMap() {
		return new TreeMap<Object, Object>(getComparator());
	}

	private static Comparator<Object> getComparator() {
		return new Comparator<Object>() {
			@SuppressWarnings("unchecked")
			@Override
			public int compare(final Object o1, final Object o2) {
				final Comparable<Object> c1 = (Comparable<Object>) o1;
				final Comparable<Object> c2 = (Comparable<Object>) o2;
				if (o1.getClass().equals(o2.getClass())) {
					return c1.compareTo(c2);
				} else {
					return c1.getClass().getName()
							.compareTo(c2.getClass().getName());
				}
			}
		};
	}
}
